package com.godpaper.as9.commands
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.godpaper.as9.bussiness.delegates.PhotoDelegate;
	import com.godpaper.as9.constants.UploadInfoList;
	import com.godpaper.as9.events.FindPhotosByNameEvent;
	import com.godpaper.as9.model.PhotoModelLocator;
	import com.godpaper.as9.vo.PhotoVO;
	
	import mx.rpc.IResponder;

	public class FindPhotosByNameCommand extends CommandBase implements ICommand, IResponder
	{
		private var delegate:PhotoDelegate;
		private var findPhotosByNameEvent:FindPhotosByNameEvent;
		private var photoModel:PhotoModelLocator = PhotoModelLocator.getInstance();
		
		public function FindPhotosByNameCommand()
		{
			super();
			delegate = new PhotoDelegate(this);
		}
		
		override public function execute(event:CairngormEvent):void
		{
			findPhotosByNameEvent = FindPhotosByNameEvent(event);
			delegate.findPhotosByName(findPhotosByNameEvent.name);
		}
		
		override public function result(data:Object):void
		{
//				Alert.show(data.result.toString());
//				photoModel.photoListBySearch.source = data.result;
			photoModel.photoListBySearch.removeAll();
			for(var i:int=0;i<data.result.length;i++)
			{
				var vo:PhotoVO = new PhotoVO();
				
				vo.ano = data.result[i].ano;
				vo.avg_score = data.result[i].avg_score;
				vo.description = data.result[i].description;
				vo.name = data.result[i].name;
				vo.pno = data.result[i].pno;
				vo.source = data.result[i].source;
				vo.tag = data.result[i].tag;
				vo.thumbnail = UploadInfoList.UPLOAD_DEST_BASE.
					concat( data.result[i].source.split("/")[2],
					"/tn/",
					data.result[i].source.split("/")[3]);
				vo.upload_time = data.result[i].upload_time;
				vo.uploader = data.result[i].uploader;
				vo.capcity = data.result[i].capcity;
				
				photoModel.photoListBySearch.addItem(vo);
			}
			photoModel.photoListBySearch.refresh(); 
			
		}
		
	}
}